<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { GetCategoryService, DelCategoryService } from '../../api/vegeFru'
import CategoryEdit from './components/CategoryEdit.vue'
const loading = ref(false)
const dialog = ref()
const categoryList = ref([])
const getCategoryList = async () => {
  loading.value = true
  const res = await GetCategoryService()
  loading.value = false
  categoryList.value = res.data
}
getCategoryList()

const onAddCategory = () => {
  dialog.value.open({})
}
const onEditCategory = (row) => {
  dialog.value.open(row)
}
const onDelCategory = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await DelCategoryService(row.id)
  await getCategoryList()
  ElMessage({ type: 'success', message: '删除成功' })
}
const onSuccess = async () => {
  await getCategoryList()
}
</script>
<template>
  <page-container title="果蔬品种管理">
    <template #extra>
      <el-button type="primary" @click="onAddCategory">添加果蔬品种</el-button>
    </template>

    <el-table :data="categoryList" style="width: 100%" v-loading="loading">
      <el-table-column label="序号" width="100" type="index"></el-table-column>
      <el-table-column label="分类名称" prop="mt"></el-table-column>
      <el-table-column label="品种" prop="st"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditCategory(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelCategory(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <category-edit ref="dialog" @success="onSuccess"></category-edit>
  </page-container>
</template>
